<?php

namespace App\Services;

if (is_file(__DIR__ . '/../autoload.php')) {
    require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';
}

use Illuminate\Support\Facades\Request;
use OSS\OssClient;
use OSS\Core\OssException;

class OssUploadFile
{
    private $accessKeyId;
    private $accessKeySecret;
    private $endpoint;
    private $bucket;
    private $request;
    public function __construct(Request $request)
    {
       $this->request=$request;
       $this->accessKeyId=env('ALIYUN_ACCESS_ID','LTAI5tJtzaaVkqxAdkpL1io3');
       $this->accessKeySecret=env('ALIYUN_ACCESS_KEY','xCsonAhaZirCC3YKEZtXkeAkqiLtog');
       $this->endpoint=env('ALIYUN_BUCKET','train-house');
       $this->bucket=env('ALIYUN_ENDPOINT','oss-cn-shanghai.aliyuncs.com');
    }
    public function fileUpload()
    {
        $object = bcrypt(time()).'.png' ;
// <yourLocalFile>由本地文件路径加文件名包括后缀组成，例如/users/local/myfile.txt。
// 填写本地文件的完整路径，例如D:\\localpath\\examplefile.txt。如果未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件。
        $filePath = "D:\\localpath\\examplefile.txt";

        try{
            $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);

            $ossClient->uploadFile($this->bucket, $object, $filePath);
        } catch(OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
    }
}
